package com.trg.designmodel.singleleton;

/**
 * @ClassName: SingleLetonTest02
 * @Description: 单例设计模式-饿汉式（静态代码块）
 * @Author: tangrg
 * @Date: 2021/6/30 14:58
 * @Version: 1.0
 * @Return:
 **/
public class SingleLetonTest02 {

    public static void main(String[] args) {
        SingleLeton02 instance = SingleLeton02.getInstance();
        SingleLeton02 instance2 = SingleLeton02.getInstance();
        System.out.println(instance == instance2);
        System.out.println("instance:"+instance.hashCode());
        System.out.println("instance2:"+instance2.hashCode());
    }

}

class SingleLeton02 {
    //***1、私有化构造器
    private SingleLeton02() {
    }

    private static  SingleLeton02 instance;

    //***2、在静态代码块中创建实例对象
    static {
        instance = new SingleLeton02();
    }

    //***3、提供一个公有的静态方法，将实例对象返回
    public static SingleLeton02 getInstance() {
        return instance;
    }
}
